<div class="selenium-notes">
<h1>Scroll Page in Selenium WebDriver</h1>
<p>Scrolling a web page is an important browser automation operation in Selenium WebDriver. Modern websites often contain long pages, dynamic sections, product listings, forms, tables, dashboards, footer sections, and lazy-loaded content that may not be visible in the current browser viewport. Selenium automation may therefore need to scroll the page before interacting with or validating specific elements.</p>
<p>In Selenium with Java, page scrolling can be handled using Selenium's Actions API or JavaScriptExecutor. JavaScriptExecutor is especially useful when the automation script needs precise control over the browser's scroll position or needs to scroll directly to a particular WebElement.</p>
<p><strong>Course Resource:</strong> <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training Course</a> | <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a></p>
<hr>
<h2>1. What is Scroll Page in Selenium?</h2>
<p>Scroll Page in Selenium means moving the browser viewport vertically or horizontally so that content outside the currently visible area becomes accessible.</p>
<p>For example, if a Submit button is located at the bottom of a long registration page, Selenium may need to scroll the page until the button is visible before performing an action.</p>
<pre><code>Open Web Page
↓
Locate Target Element
↓
Check Current Viewport
↓
Scroll Page
↓
Target Becomes Visible
↓
Perform Action
↓
Validate Result</code></pre>
<h2>2. Why is Page Scrolling Required?</h2>
<p>Scrolling is useful whenever the target content is outside the current viewport or when scrolling itself triggers additional application behavior.</p>
<ul>
<li>To access elements below the visible viewport.</li>
<li>To interact with buttons at the bottom of long pages.</li>
<li>To test long registration forms.</li>
<li>To validate footer sections.</li>
<li>To test product listings.</li>
<li>To work with large web tables.</li>
<li>To trigger lazy-loaded content.</li>
<li>To test infinite-scroll pages.</li>
<li>To validate dynamic web applications.</li>
<li>To capture screenshots of specific page sections.</li>
<li>To test responsive layouts.</li>
</ul>
<h2>3. Selenium Scrolling Techniques</h2>
<p>There are several approaches to scrolling in Selenium.</p>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Technique</th><th>Purpose</th></tr>
<tr><td>JavaScriptExecutor</td><td>Programmatically control browser scrolling</td></tr>
<tr><td>Actions.scrollByAmount()</td><td>Scroll by a specific amount</td></tr>
<tr><td>Actions.scrollToElement()</td><td>Scroll directly to an element</td></tr>
<tr><td>scrollIntoView()</td><td>Bring a particular DOM element into the viewport</td></tr>
<tr><td>Container scroll</td><td>Scroll an internal div or panel</td></tr>
<tr><td>Keyboard actions</td><td>Use PAGE_DOWN, PAGE_UP, HOME and END</td></tr>
</table>
<h2>4. What is JavascriptExecutor?</h2>
<p><strong>JavascriptExecutor</strong> is a Selenium interface that allows Java Selenium programs to execute JavaScript inside the browser's current page context.</p>
<p>It is commonly used for specialized browser operations such as scrolling, reading page properties, interacting with DOM properties, checking page readiness, highlighting elements, and performing other JavaScript-based operations.</p>
<h2>5. Import JavascriptExecutor</h2>
<pre><code>import org.openqa.selenium.JavascriptExecutor;</code></pre>
<h2>6. Create JavascriptExecutor Object</h2>
<pre><code>JavascriptExecutor js = (JavascriptExecutor) driver;</code></pre>
<p>The WebDriver object is type-cast to <code>JavascriptExecutor</code> so JavaScript can be executed through Selenium.</p>
<h2>7. Scroll Down Using JavaScript</h2>
<p>The <code>window.scrollBy()</code> method can be used to move the browser viewport by a specified number of pixels.</p>
<pre><code>JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500);");</code></pre>
<p>Here, <code>0</code> represents horizontal movement and <code>500</code> represents vertical movement.</p>
<h2>8. Scroll Down by 1000 Pixels</h2>
<pre><code>js.executeScript("window.scrollBy(0,1000);");</code></pre>
<p>This moves the page approximately 1000 pixels downward.</p>
<h2>9. Scroll Up the Page</h2>
<p>A negative vertical value can be used to scroll upward.</p>
<pre><code>js.executeScript("window.scrollBy(0,-500);");</code></pre>
<h2>10. Scroll Horizontally</h2>
<p>Horizontal scrolling can be performed by changing the first value.</p>
<pre><code>js.executeScript("window.scrollBy(500,0);");</code></pre>
<h2>11. Scroll to the Top of the Page</h2>
<p>The <code>window.scrollTo()</code> method can move the page directly to a specific coordinate.</p>
<pre><code>js.executeScript("window.scrollTo(0,0);");</code></pre>
<h2>12. Scroll to the Bottom of the Page</h2>
<pre><code>js.executeScript(
"window.scrollTo(0, document.body.scrollHeight);"
);</code></pre>
<p><code>document.body.scrollHeight</code> represents the total height of the page content.</p>
<h2>13. Difference Between scrollBy() and scrollTo()</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Method</th><th>Meaning</th><th>Example</th></tr>
<tr><td>scrollBy()</td><td>Moves relative to the current position</td><td><code>window.scrollBy(0,500)</code></td></tr>
<tr><td>scrollTo()</td><td>Moves to an absolute position</td><td><code>window.scrollTo(0,1000)</code></td></tr>
</table>
<h2>14. Scroll to a Specific WebElement</h2>
<p>Instead of guessing the number of pixels, Selenium can scroll directly to a target element.</p>
<pre><code>WebElement element = driver.findElement(By.id("footer"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(
"arguments[0].scrollIntoView(true);",
element
);</code></pre>
<h2>15. Understanding scrollIntoView()</h2>
<p>The JavaScript <code>scrollIntoView()</code> method scrolls the page until the specified DOM element is visible in the viewport.</p>
<pre><code>arguments[0].scrollIntoView(true);</code></pre>
<p>When the element is passed as an argument from Selenium, it becomes <code>arguments[0]</code> inside the JavaScript code.</p>
<h2>16. Understanding arguments[0]</h2>
<p><code>arguments[0]</code> represents the first argument supplied to JavaScriptExecutor.</p>
<pre><code>WebElement button = driver.findElement(By.id("submit"));
js.executeScript(
"arguments[0].scrollIntoView(true);",
button
);</code></pre>
<p>Here, <code>button</code> is passed as the first argument and becomes <code>arguments[0]</code>.</p>
<h2>17. Scroll Element to Center</h2>
<p>Centering an element can be useful on pages with sticky headers or fixed navigation bars.</p>
<pre><code>WebElement element = driver.findElement(By.id("submit"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>18. Scroll Element to Top</h2>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'start'});",
element
);</code></pre>
<h2>19. Scroll Element to Bottom</h2>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'end'});",
element
);</code></pre>
<h2>20. Scroll Element with Nearest Alignment</h2>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'nearest'});",
element
);</code></pre>
<p>The <code>nearest</code> option attempts to move the minimum amount necessary to make the element visible.</p>
<h2>21. Scroll Element Horizontally and Vertically</h2>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'center', inline:'center'});",
element
);</code></pre>
<h2>22. Scrolling with Selenium Actions</h2>
<p>Selenium's Actions class provides scrolling capabilities through WebDriver input actions.</p>
<pre><code>Actions actions = new Actions(driver);
actions.scrollByAmount(0, 500).perform();</code></pre>
<h2>23. Required Actions Import</h2>
<pre><code>import org.openqa.selenium.interactions.Actions;</code></pre>
<h2>24. Scroll to Element Using Actions</h2>
<pre><code>WebElement element = driver.findElement(By.id("target"));
Actions actions = new Actions(driver);
actions.scrollToElement(element).perform();</code></pre>
<h2>25. JavaScript Scroll vs Actions Scroll</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Feature</th><th>JavaScriptExecutor</th><th>Actions</th></tr>
<tr><td>Pixel scrolling</td><td>Yes</td><td>Yes</td></tr>
<tr><td>Scroll to element</td><td>scrollIntoView()</td><td>scrollToElement()</td></tr>
<tr><td>DOM JavaScript execution</td><td>Yes</td><td>No</td></tr>
<tr><td>Browser-side scripting</td><td>Yes</td><td>No</td></tr>
<tr><td>Input action model</td><td>Not its main purpose</td><td>Yes</td></tr>
</table>
<h2>26. Scroll Using Keyboard Actions</h2>
<p>Keyboard keys can also be used for page navigation.</p>
<pre><code>Actions actions = new Actions(driver);
actions.sendKeys(Keys.PAGE_DOWN).perform();</code></pre>
<h2>27. Scroll Up Using PAGE_UP</h2>
<pre><code>actions.sendKeys(Keys.PAGE_UP).perform();</code></pre>
<h2>28. Move to Top Using HOME</h2>
<pre><code>actions.sendKeys(Keys.HOME).perform();</code></pre>
<h2>29. Move to Bottom Using END</h2>
<pre><code>actions.sendKeys(Keys.END).perform();</code></pre>
<h2>30. Keyboard Scrolling Comparison</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Key</th><th>Typical Purpose</th></tr>
<tr><td>PAGE_DOWN</td><td>Move viewport downward</td></tr>
<tr><td>PAGE_UP</td><td>Move viewport upward</td></tr>
<tr><td>HOME</td><td>Move toward the beginning</td></tr>
<tr><td>END</td><td>Move toward the end</td></tr>
</table>
<h2>31. Scroll Before Clicking</h2>
<p>A common Selenium scenario is scrolling an element into view before clicking it.</p>
<pre><code>WebElement button = driver.findElement(By.id("submit"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
button
);
button.click();</code></pre>
<h2>32. Scroll Before Typing</h2>
<pre><code>WebElement address = driver.findElement(By.id("address"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
address
);
address.sendKeys("Mumbai");</code></pre>
<h2>33. Scroll Before Selecting a Dropdown</h2>
<pre><code>WebElement country = driver.findElement(By.id("country"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
country
);
Select select = new Select(country);
select.selectByVisibleText("India");</code></pre>
<h2>34. Scroll Before Checking a Checkbox</h2>
<pre><code>WebElement checkbox = driver.findElement(By.id("terms"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
checkbox
);
if (!checkbox.isSelected()) {
checkbox.click();
}</code></pre>
<h2>35. Scroll Before Selecting a Radio Button</h2>
<pre><code>WebElement radio = driver.findElement(By.id("male"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
radio
);
radio.click();</code></pre>
<h2>36. Scroll Before Taking Screenshot</h2>
<p>Scrolling can be used to position a specific page section before capturing evidence.</p>
<pre><code>WebElement section = driver.findElement(By.id("pricing"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
section
);
File screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);</code></pre>
<h2>37. Scroll to Footer</h2>
<pre><code>WebElement footer = driver.findElement(
By.tagName("footer")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
footer
);</code></pre>
<h2>38. Scroll to Web Table</h2>
<pre><code>WebElement table = driver.findElement(
By.id("employeeTable")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
table
);</code></pre>
<h2>39. Scroll to a Form</h2>
<pre><code>WebElement form = driver.findElement(
By.id("registrationForm")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
form
);</code></pre>
<h2>40. Scroll to a Link</h2>
<pre><code>WebElement link = driver.findElement(
By.linkText("Contact Us")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
link
);</code></pre>
<h2>41. Scroll to a Product</h2>
<pre><code>WebElement product = driver.findElement(
By.xpath("//h2[text()='Laptop']")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
product
);</code></pre>
<h2>42. Scrolling in E-Commerce Testing</h2>
<p>E-commerce applications often contain long product pages and product listings. Automation may need to scroll to product details, reviews, recommendations, related products, filters, or checkout controls.</p>
<pre><code>WebElement reviews = driver.findElement(
By.id("customer-reviews")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
reviews
);
Assert.assertTrue(reviews.isDisplayed());</code></pre>
<h2>43. Scrolling in Registration Testing</h2>
<p>Long registration forms may contain address, employment, preferences, terms, and submission sections below the initial viewport.</p>
<pre><code>WebElement address = driver.findElement(By.id("address"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
address
);
address.sendKeys("Mumbai");</code></pre>
<h2>44. Scrolling in Checkout Testing</h2>
<pre><code>WebElement placeOrder = driver.findElement(
By.id("place-order")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
placeOrder
);
placeOrder.click();</code></pre>
<h2>45. Scrolling and Explicit Wait</h2>
<p>Scrolling does not replace synchronization. A page may continue loading even after the browser has navigated to it. Explicit waits should be used when the target element depends on dynamic application behavior.</p>
<pre><code>WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("target")
)
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>46. Scroll and Clickable Element</h2>
<pre><code>WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
button
);
button.click();</code></pre>
<h2>47. Why Explicit Wait is Important</h2>
<ul>
<li>The element may not have been created yet.</li>
<li>The element may not be visible.</li>
<li>The page may still be loading dynamic content.</li>
<li>An overlay may temporarily cover the element.</li>
<li>JavaScript application code may still be updating the DOM.</li>
<li>The target may become stale after a page update.</li>
</ul>
<h2>48. Scrolling and Sticky Headers</h2>
<p>Many modern websites use sticky navigation bars that remain fixed at the top of the browser while the page scrolls. A target element can therefore become technically visible but still be covered by the fixed header.</p>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView(true);",
element
);
js.executeScript("window.scrollBy(0,-100);");</code></pre>
<p>The offset should be adjusted according to the actual fixed header used by the application.</p>
<h2>49. Center Scrolling to Handle Sticky Headers</h2>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<p>Centering the element can make it easier to interact with when the top of the viewport contains fixed navigation.</p>
<h2>50. Scrolling a Specific Container</h2>
<p>Not every scroll operation affects the entire browser window. Many applications contain scrollable panels, lists, modals, sidebars, or table containers.</p>
<pre><code>WebElement container = driver.findElement(
By.id("scrollContainer")
);
js.executeScript(
"arguments[0].scrollTop = arguments[0].scrollHeight;",
container
);</code></pre>
<h2>51. Scroll Inside a Div</h2>
<pre><code>WebElement panel = driver.findElement(
By.cssSelector(".results-panel")
);
js.executeScript(
"arguments[0].scrollTop += 500;",
panel
);</code></pre>
<h2>52. Scroll Internal Div to Top</h2>
<pre><code>js.executeScript(
"arguments[0].scrollTop = 0;",
panel
);</code></pre>
<h2>53. Scroll Internal Div to Bottom</h2>
<pre><code>js.executeScript(
"arguments[0].scrollTop = arguments[0].scrollHeight;",
panel
);</code></pre>
<h2>54. Window Scroll vs Container Scroll</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Type</th><th>JavaScript Example</th></tr>
<tr><td>Window vertical scroll</td><td><code>window.scrollBy(0,500)</code></td></tr>
<tr><td>Window top</td><td><code>window.scrollTo(0,0)</code></td></tr>
<tr><td>Window bottom</td><td><code>window.scrollTo(0,document.body.scrollHeight)</code></td></tr>
<tr><td>Container vertical scroll</td><td><code>element.scrollTop</code></td></tr>
<tr><td>Container maximum scroll</td><td><code>element.scrollHeight</code></td></tr>
</table>
<h2>55. Horizontal Scrolling in a Table</h2>
<p>Large tables may be wider than the browser viewport and may require horizontal scrolling.</p>
<pre><code>WebElement tableContainer = driver.findElement(
By.cssSelector(".table-container")
);
js.executeScript(
"arguments[0].scrollLeft = arguments[0].scrollWidth;",
tableContainer
);</code></pre>
<h2>56. Horizontal Scroll by Pixels</h2>
<pre><code>js.executeScript("window.scrollBy(500,0);");</code></pre>
<h2>57. Check Current Vertical Scroll Position</h2>
<pre><code>Long position = (Long) js.executeScript(
"return window.pageYOffset;"
);
System.out.println("Scroll Position: " + position);</code></pre>
<h2>58. Check Page Height</h2>
<pre><code>Long height = (Long) js.executeScript(
"return document.body.scrollHeight;"
);
System.out.println("Page Height: " + height);</code></pre>
<h2>59. Check Viewport Height</h2>
<pre><code>Long viewportHeight = (Long) js.executeScript(
"return window.innerHeight;"
);
System.out.println(
"Viewport Height: " + viewportHeight
);</code></pre>
<h2>60. Page Height vs Viewport Height</h2>
<pre><code>Complete Page Height
↓
+---------------------------+
| |
| Page Content |
| |
|---------------------------|
| Visible Viewport |
|---------------------------|
| |
| More Content |
| |
+---------------------------+
Page Height > Viewport Height
↓
Scroll</code></pre>
<h2>61. Infinite Scrolling</h2>
<p>Infinite scrolling is a technique in which additional content is loaded as the user reaches the bottom or near-bottom of a page. Social feeds, product catalogs, news pages, and media applications commonly use this pattern.</p>
<pre><code>Scroll Down
↓
Reach Bottom
↓
Application Requests More Data
↓
New Content Appears
↓
Page Height Changes
↓
Scroll Again
↓
Repeat Until Condition Is Met</code></pre>
<h2>62. Basic Infinite Scroll Example</h2>
<pre><code>JavascriptExecutor js = (JavascriptExecutor) driver;
long previousHeight = (Long) js.executeScript(
"return document.body.scrollHeight;"
);
while (true) {
js.executeScript(
"window.scrollTo(0, document.body.scrollHeight);"
);
Thread.sleep(1000);
long currentHeight = (Long) js.executeScript(
"return document.body.scrollHeight;"
);
if (currentHeight == previousHeight) {
break;
}
previousHeight = currentHeight;
}</code></pre>
<p>This example demonstrates the basic mechanism. In production automation, explicit synchronization based on the application's loading condition is generally preferable to relying only on a fixed sleep.</p>
<h2>63. Infinite Scroll with Target Element</h2>
<p>A practical test should usually stop scrolling when the required content is found instead of scrolling indefinitely.</p>
<pre><code>JavascriptExecutor js = (JavascriptExecutor) driver;
for (int i = 0; i < 20; i++) {
List<WebElement> products =
driver.findElements(By.cssSelector(".product"));
boolean found = false;
for (WebElement product : products) {
if (product.getText().contains("Laptop")) {
found = true;
break;
}
}
if (found) {
break;
}
js.executeScript(
"window.scrollBy(0,700);"
);
}</code></pre>
<h2>64. Lazy-Loaded Content</h2>
<p>Lazy loading means that some page resources or content are loaded only when they approach the visible area. Scrolling can trigger the application to load these resources.</p>
<pre><code>js.executeScript(
"window.scrollTo(0, document.body.scrollHeight);"
);</code></pre>
<p>After scrolling, the test should wait for the expected content or state rather than assuming that the content has loaded immediately.</p>
<h2>65. Smooth Scrolling</h2>
<p>JavaScript can request smooth scrolling in browsers that support the relevant scrolling behavior.</p>
<pre><code>js.executeScript(
"window.scrollTo({top:1000, behavior:'smooth'});"
);</code></pre>
<p>Smooth scrolling is more relevant to visual behavior. Functional automation generally benefits from deterministic scrolling and appropriate synchronization.</p>
<h2>66. Scroll and Visibility Verification</h2>
<pre><code>WebElement element = driver.findElement(By.id("target"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);
Assert.assertTrue(element.isDisplayed());</code></pre>
<h2>67. Scroll and Perform Mouse Action</h2>
<pre><code>WebElement element = driver.findElement(By.id("target"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();</code></pre>
<h2>68. Scroll with CSS Selector</h2>
<pre><code>WebElement element = driver.findElement(
By.cssSelector("#footer")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>69. Scroll with XPath</h2>
<pre><code>WebElement element = driver.findElement(
By.xpath("//button[text()='Submit']")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>70. Scroll with Name Locator</h2>
<pre><code>WebElement element = driver.findElement(
By.name("address")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>71. Scroll with Class Name</h2>
<pre><code>WebElement element = driver.findElement(
By.className("footer")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>72. Scroll and Validate Text</h2>
<pre><code>WebElement footer = driver.findElement(
By.cssSelector("footer")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
footer
);
Assert.assertTrue(
footer.getText().contains("Contact")
);</code></pre>
<h2>73. Scroll and Validate URL</h2>
<pre><code>WebElement link = driver.findElement(
By.linkText("Contact Us")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
link
);
link.click();
Assert.assertTrue(
driver.getCurrentUrl().contains("contact")
);</code></pre>
<h2>74. Scroll and Validate Page Title</h2>
<pre><code>WebElement button = driver.findElement(By.id("dashboard"));
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
button
);
button.click();
Assert.assertEquals(
driver.getTitle(),
"Dashboard"
);</code></pre>
<h2>75. Scrolling and ElementClickInterceptedException</h2>
<p><code>ElementClickInterceptedException</code> may occur when another element, popup, overlay, or fixed component is positioned over the target.</p>
<p>Scrolling the target into an appropriate position and waiting for temporary overlays to disappear can often address the problem. JavaScript click should not automatically be used as the first response because the underlying issue may be synchronization or an application-state problem.</p>
<h2>76. Scrolling and StaleElementReferenceException</h2>
<p>Dynamic applications may replace DOM elements while the page is being scrolled. If a previously located element becomes stale, locate the element again.</p>
<pre><code>WebElement element = wait.until(
ExpectedConditions.presenceOfElementLocated(
By.id("target")
)
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>77. Scrolling and NoSuchElementException</h2>
<p>If the target element cannot be found, scrolling does not solve the locator problem. Verify the locator, page state, frame context, and timing.</p>
<h2>78. Scrolling and ElementNotInteractableException</h2>
<p>An element can exist in the DOM but still not be ready for interaction. Check visibility, enabled state, overlays, frames, and application synchronization.</p>
<h2>79. Scrolling Inside an iframe</h2>
<p>If the target element is inside an iframe, Selenium must first switch into the correct frame.</p>
<pre><code>WebElement frame = driver.findElement(
By.id("paymentFrame")
);
driver.switchTo().frame(frame);
WebElement button = driver.findElement(
By.id("submit")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
button
);
button.click();
driver.switchTo().defaultContent();</code></pre>
<h2>80. Scrolling and Multiple Windows</h2>
<p>Scrolling applies to the currently active browser window or tab. If the target is in another window, switch to that window first.</p>
<pre><code>driver.switchTo().window(windowHandle);
js.executeScript(
"window.scrollTo(0, document.body.scrollHeight);"
);</code></pre>
<h2>81. Scrolling in Responsive Testing</h2>
<p>Responsive layouts may change element positions based on browser width and height. Therefore, tests should avoid relying on fixed screen coordinates.</p>
<pre><code>driver.manage().window().setSize(
new Dimension(390, 844)
);</code></pre>
<p>After changing the viewport, the test should locate elements again and use element-based scrolling where appropriate.</p>
<h2>82. Scrolling in Headless Testing</h2>
<p>Headless browser tests still have a viewport and page scrolling behavior even though the browser window is not displayed to the user.</p>
<pre><code>ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
options.addArguments("--window-size=1920,1080");
WebDriver driver = new ChromeDriver(options);</code></pre>
<h2>83. Scrolling and Browser Zoom</h2>
<p>Browser zoom can affect the visual relationship between coordinates and elements. Element-based scrolling is generally more robust than coordinate-based automation.</p>
<h2>84. Scrolling and Page Object Model</h2>
<p>In a Page Object Model framework, page-specific scrolling can be encapsulated in page methods while generic scrolling functionality can be placed inside a reusable utility class.</p>
<pre><code>public void scrollToSubmitButton() {
WebElement button = driver.findElement(
By.id("submit")
);
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
button
);
}</code></pre>
<h2>85. Reusable Scroll Utility</h2>
<pre><code>public class ScrollUtils {
private WebDriver driver;
public ScrollUtils(WebDriver driver) {
this.driver = driver;
}
public void scrollDown(int pixels) {
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"window.scrollBy(0," + pixels + ");"
);
}
public void scrollUp(int pixels) {
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"window.scrollBy(0,-" + pixels + ");"
);
}
public void scrollToTop() {
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"window.scrollTo(0,0);"
);
}
public void scrollToBottom() {
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"window.scrollTo(0,document.body.scrollHeight);"
);
}
public void scrollToElement(WebElement element) {
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);
}
}</code></pre>
<h2>86. Using Scroll Utility</h2>
<pre><code>ScrollUtils scroll = new ScrollUtils(driver);
scroll.scrollDown(500);
scroll.scrollToElement(
driver.findElement(By.id("footer"))
);
scroll.scrollToBottom();
scroll.scrollToTop();</code></pre>
<h2>87. Complete Selenium Scroll Example</h2>
<pre><code>import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ScrollPageExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.manage().window().maximize();
driver.get("https://example.com");
WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement footer = wait.until(
ExpectedConditions.presenceOfElementLocated(
By.tagName("footer")
)
);
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
footer
);
wait.until(
ExpectedConditions.visibilityOf(footer)
);
System.out.println(
"Footer is visible: "
+ footer.isDisplayed()
);
} finally {
driver.quit();
}
}
}</code></pre>
<h2>88. Scroll Page with TestNG</h2>
<pre><code>import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public void verifyFooterAfterScrolling() {
WebElement footer = driver.findElement(
By.tagName("footer")
);
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
footer
);
Assert.assertTrue(
footer.isDisplayed()
);
}</code></pre>
<h2>89. Practical Test Scenario</h2>
<ol>
<li>Launch the browser.</li>
<li>Open the application.</li>
<li>Maximize or configure the required viewport.</li>
<li>Locate the target element.</li>
<li>Wait for the element when necessary.</li>
<li>Scroll the element into view.</li>
<li>Verify visibility.</li>
<li>Perform the required action.</li>
<li>Validate the expected result.</li>
<li>Capture evidence if required.</li>
<li>Close the browser.</li>
</ol>
<h2>90. Real-World Project Example: E-Commerce</h2>
<p>Suppose an e-commerce product page contains product information at the top, specifications in the middle, customer reviews below, and related products near the bottom.</p>
<pre><code>WebElement reviews = driver.findElement(
By.id("customer-reviews")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
reviews
);
Assert.assertTrue(
reviews.isDisplayed()
);</code></pre>
<h2>91. Real-World Project Example: Registration</h2>
<pre><code>WebElement address = driver.findElement(
By.id("address")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
address
);
address.sendKeys("Mumbai");
WebElement submit = driver.findElement(
By.id("submit")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
submit
);
submit.click();</code></pre>
<h2>92. Real-World Project Example: Footer Links</h2>
<pre><code>WebElement footer = driver.findElement(
By.tagName("footer")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
footer
);
List<WebElement> links =
footer.findElements(By.tagName("a"));
System.out.println(
"Footer Links: " + links.size()
);</code></pre>
<h2>93. Real-World Project Example: Long Dashboard</h2>
<p>A dashboard may contain summary cards at the top and reports, charts, tables, and logs farther down the page.</p>
<pre><code>WebElement reportTable = driver.findElement(
By.id("reportTable")
);
js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
reportTable
);
Assert.assertTrue(
reportTable.isDisplayed()
);</code></pre>
<h2>94. Real-World Project Example: News Website</h2>
<p>A news website may use infinite scrolling to load additional articles. Selenium can repeatedly scroll, wait for additional articles, inspect the loaded content, and stop when the required article is found.</p>
<pre><code>for (int i = 0; i < 10; i++) {
List<WebElement> articles =
driver.findElements(By.cssSelector("article"));
boolean found = false;
for (WebElement article : articles) {
if (article.getText().contains("Selenium")) {
found = true;
break;
}
}
if (found) {
break;
}
js.executeScript(
"window.scrollBy(0,700);"
);
}</code></pre>
<h2>95. Common Mistake: Using Large Fixed Scroll Values</h2>
<p>A command such as <code>window.scrollBy(0,5000)</code> may work on one screen size but behave differently on another page or viewport.</p>
<p>When a target element is known, prefer:</p>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>96. Common Mistake: Using Thread.sleep() Everywhere</h2>
<p>Fixed sleeps can make tests slower and may still fail when an application takes longer than the chosen delay.</p>
<p>Use explicit waits for application conditions whenever possible.</p>
<h2>97. Common Mistake: Assuming Visible Means Clickable</h2>
<p>An element can be visible but still be covered by an overlay, disabled, stale, or not ready for interaction.</p>
<pre><code>WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);</code></pre>
<h2>98. Common Mistake: Ignoring Scrollable Containers</h2>
<p>If the page itself does not move when scrolling, inspect the application for an internal scrollable container.</p>
<pre><code>WebElement panel = driver.findElement(
By.cssSelector(".results-panel")
);
js.executeScript(
"arguments[0].scrollTop = arguments[0].scrollHeight;",
panel
);</code></pre>
<h2>99. Common Mistake: Using Coordinates</h2>
<p>Coordinate-based scrolling and clicking can become unreliable because browser size, responsive layouts, zoom, operating system settings, and page changes can affect coordinates.</p>
<p>Stable locators and element-based operations are generally easier to maintain.</p>
<h2>100. Common Mistake: Using JavaScript for Everything</h2>
<p>JavaScriptExecutor is powerful but should not automatically replace Selenium's normal APIs.</p>
<p>For normal interactions, prefer Selenium methods when they work correctly:</p>
<pre><code>element.click();
element.sendKeys("text");
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();</code></pre>
<h2>101. Best Practice: Scroll to the Target Element</h2>
<p>When a specific element is the subject of the test, element-based scrolling is usually more maintainable than guessing a pixel amount.</p>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<h2>102. Best Practice: Use Explicit Synchronization</h2>
<pre><code>WebElement target = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("target")
)
);</code></pre>
<h2>103. Best Practice: Consider Fixed Headers</h2>
<p>Use center alignment or a small scroll offset when fixed headers cover elements after scrolling.</p>
<h2>104. Best Practice: Validate the Business Result</h2>
<p>Scrolling is usually an intermediate step. The test should validate the actual expected behavior after scrolling.</p>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
button
);
button.click();
Assert.assertTrue(
driver.getCurrentUrl().contains("dashboard")
);</code></pre>
<h2>105. Best Practice: Re-Locate Dynamic Elements</h2>
<p>When scrolling causes the application to refresh or replace DOM elements, locate the required element again instead of assuming the old WebElement reference remains valid.</p>
<h2>106. Best Practice: Keep Scroll Methods Reusable</h2>
<p>Common scrolling operations such as scroll top, scroll bottom, scroll by pixels, and scroll to element can be placed in reusable utility classes.</p>
<h2>107. Best Practice: Handle Frames Correctly</h2>
<p>If the target element is inside an iframe, switch to the correct frame before locating and scrolling to the element.</p>
<h2>108. Best Practice: Test Different Viewports</h2>
<p>Responsive pages can change layout and scrolling behavior at different browser dimensions. When required, execute the same scrolling scenario at multiple viewport sizes.</p>
<h2>109. Selenium Scroll Page Interview Questions</h2>
<p><strong>Q1. How do you scroll down a webpage in Selenium?</strong></p>
<pre><code>JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500);");</code></pre>
<p><strong>Q2. How do you scroll to the bottom of a webpage?</strong></p>
<pre><code>js.executeScript(
"window.scrollTo(0, document.body.scrollHeight);"
);</code></pre>
<p><strong>Q3. How do you scroll to the top?</strong></p>
<pre><code>js.executeScript("window.scrollTo(0,0);");</code></pre>
<p><strong>Q4. How do you scroll to a specific WebElement?</strong></p>
<pre><code>js.executeScript(
"arguments[0].scrollIntoView({block:'center'});",
element
);</code></pre>
<p><strong>Q5. What is the difference between scrollBy() and scrollTo()?</strong></p>
<p><code>scrollBy()</code> moves the viewport relative to its current position, while <code>scrollTo()</code> moves it to an absolute position.</p>
<p><strong>Q6. Can Selenium scroll without JavaScript?</strong></p>
<p>Yes. Selenium's Actions API provides scrolling operations, including scrolling by an amount and scrolling to an element.</p>
<p><strong>Q7. How do you scroll inside a div?</strong></p>
<pre><code>js.executeScript(
"arguments[0].scrollTop = arguments[0].scrollHeight;",
element
);</code></pre>
<p><strong>Q8. What is infinite scrolling?</strong></p>
<p>Infinite scrolling is a page design where additional content is loaded as the user approaches the bottom of the page.</p>
<p><strong>Q9. Does scrolling guarantee that an element is clickable?</strong></p>
<p>No. The element may still be covered, disabled, stale, or affected by application synchronization issues.</p>
<p><strong>Q10. Why is scrollIntoView() useful?</strong></p>
<p>It allows automation to bring a specific element into the viewport without relying on an arbitrary pixel distance.</p>
<h2>110. Quick Revision Table</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Requirement</th><th>Code</th></tr>
<tr><td>Scroll down</td><td><code>window.scrollBy(0,500)</code></td></tr>
<tr><td>Scroll up</td><td><code>window.scrollBy(0,-500)</code></td></tr>
<tr><td>Scroll top</td><td><code>window.scrollTo(0,0)</code></td></tr>
<tr><td>Scroll bottom</td><td><code>window.scrollTo(0,document.body.scrollHeight)</code></td></tr>
<tr><td>Scroll to element</td><td><code>arguments[0].scrollIntoView()</code></td></tr>
<tr><td>Scroll using Actions</td><td><code>actions.scrollByAmount(0,500).perform()</code></td></tr>
<tr><td>Scroll to element using Actions</td><td><code>actions.scrollToElement(element).perform()</code></td></tr>
<tr><td>Container scroll</td><td><code>arguments[0].scrollTop</code></td></tr>
</table>
<h2>111. Scroll Page Automation Checklist</h2>
<ul>
<li>Identify whether the browser window or an internal container needs scrolling.</li>
<li>Locate the target element using a stable locator.</li>
<li>Use explicit waits for dynamic content.</li>
<li>Prefer element-based scrolling when the target is known.</li>
<li>Consider sticky headers and fixed overlays.</li>
<li>Handle iframes and windows correctly.</li>
<li>Use Actions when input-based scrolling is appropriate.</li>
<li>Use JavaScriptExecutor when precise browser-side scrolling is required.</li>
<li>Avoid unnecessary fixed sleeps.</li>
<li>Avoid coordinate-based automation when stable element locators are available.</li>
<li>Validate the expected result after scrolling.</li>
<li>Keep reusable scrolling operations in utility methods.</li>
</ul>
<h2>112. Complete Scroll Page Flow</h2>
<pre><code>Start Test
↓
Launch Browser
↓
Open Application
↓
Identify Target
↓
Wait for Required Condition
↓
Determine Scroll Type
↓
┌──────────────────────────────┐
│ Window / Element / Container │
└──────────────────────────────┘
↓
Scroll Into Required Position
↓
Verify Visibility / State
↓
Perform Action
↓
Validate Result
↓
Capture Evidence
↓
End Test</code></pre>
<h2>113. Selenium Training and Learning Resource</h2>
<p>JustAcademy's Selenium Automation Testing course provides training resources covering Selenium WebDriver, Java automation, locators, browser automation, TestNG, data-driven testing, Page Object Model, cross-browser testing, automation frameworks, practical projects, and related testing tools.</p>
<p><a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training Course</a></p>
<p><a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a></p>
<hr>
<h2>114. Conclusion</h2>
<p>Page scrolling is an essential Selenium WebDriver skill for testing long, dynamic, responsive, and content-heavy web applications. Selenium provides multiple approaches, including JavaScriptExecutor, Actions scrolling, keyboard actions, element-based scrolling, and scrolling inside individual containers.</p>
<p>For reliable automation, the focus should not be only on moving the page. A good Selenium test identifies the correct target, waits for the required application state, scrolls the target into a suitable viewport position, performs the intended interaction, and validates the expected result.</p>
<p>By practicing scroll-by-pixels, scroll-to-top, scroll-to-bottom, scroll-to-element, container scrolling, horizontal scrolling, infinite scrolling, lazy-loaded content, and scrolling with explicit waits, testers can handle a wide range of real-world Selenium automation scenarios.</p>
</div>